<template>
  <div class="typescript-demo">
    <h2>Vue 3 TypeScript 支持</h2>
    
    <div class="ts-features">
      <!-- 类型推导 -->
      <div class="feature-card">
        <h3>🧩 强大的类型推导</h3>
        <div class="content">
          <p>Vue 3 与 TypeScript 深度集成，提供了优秀的类型推导能力，使开发过程更加安全和高效。</p>
          <div class="code-block">
            <CodeBlock :code="typeInferenceCode" language="typescript"></CodeBlock>
          </div>
        </div>
      </div>

      <!-- 组件Props类型 -->
      <div class="feature-card">
        <h3>📝 组件Props类型</h3>
        <div class="content">
          <p>通过 TypeScript 接口定义组件 Props，可以获得完整的类型检查和编辑器智能提示。</p>
          <div class="demo">
            <props-demo 
              :user-data="userData"
              :count="count"
              :active="isActive"
            />
            <button @click="updateUserData" class="btn-primary">更新用户数据</button>
          </div>
        </div>
      </div>

      <!-- 响应式API类型支持 -->
      <div class="feature-card">
        <h3>🔄 响应式API类型支持</h3>
        <div class="content">
          <p>ref、reactive、computed 等响应式 API 都有完整的类型支持，可以自动推断或显式指定类型。</p>
          <div class="code-block">
            <CodeBlock :code="reactiveApiCode" language="typescript"></CodeBlock>
          </div>
          <div class="demo">
            <p>类型化的响应式对象: {{ typedObject.name }}, {{ typedObject.age }}岁</p>
            <button @click="updateTypedObject" class="btn-secondary">更新类型化对象</button>
          </div>
        </div>
      </div>

      <!-- 模板引用类型 -->
      <div class="feature-card">
        <h3>🎯 模板引用类型</h3>
        <div class="content">
          <p>Vue 3 支持为模板引用指定具体的 DOM 元素类型，提供精确的类型检查。</p>
          <div class="demo">
            <input 
              ref="inputRef" 
              type="text" 
              placeholder="输入内容..."
              class="input-field"
            />
            <button @click="focusInput" class="btn-primary">聚焦输入框</button>
            <button @click="getInputValue" class="btn-secondary">获取输入值</button>
            <p v-if="inputValue" class="result">输入值: {{ inputValue }}</p>
          </div>
          <div class="code-block">
            <CodeBlock :code="templateRefCode" language="typescript"></CodeBlock>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import CodeBlock from '@components/CodeBlock.vue'

// 类型定义示例
interface UserData {
  id: number
  name: string
  email: string
  role: 'admin' | 'user' | 'guest'
}

interface Product {
  id: number
  name: string
  price: number
  inStock: boolean
}

// 类型推导演示
const typeInferenceCode = `// 接口定义
interface User {
  id: number;
  name: string;
  age: number;
}

// 类型自动推导
const user = reactive<User>({
  id: 1,
  name: 'Vue 3',
  age: 3
});

// computed 自动推导返回类型
const isAdult = computed(() => {
  return user.age >= 18;
});`

// 响应式API类型代码
const reactiveApiCode = `// 显式指定 ref 类型
const count = ref<number>(0);

// reactive 使用泛型
interface Person {
  name: string;
  age: number;
}

const person = reactive<Person>({
  name: 'TypeScript',
  age: 10
});

// computed 显式指定返回类型
const greeting = computed<string>(() => {
  return \`Hello, \${person.name}!\`;
});`

// 模板引用代码
const templateRefCode = `// 指定模板引用类型
import { ref } from 'vue';

const inputRef = ref<HTMLInputElement | null>(null);

function focusInput() {
  inputRef.value?.focus();
}

function getValue() {
  return inputRef.value?.value || '';
}`

// 组件数据
const userData = ref<UserData>({
  id: 1,
  name: '张三',
  email: 'zhangsan@example.com',
  role: 'admin'
})

const count = ref(42)
const isActive = ref(true)

// 类型化的响应式对象
const typedObject = reactive<{ name: string; age: number }>({
  name: 'TypeScript 演示',
  age: 5
})

// 模板引用
const inputRef = ref<HTMLInputElement | null>(null)
const inputValue = ref('')

// 方法
const updateUserData = () => {
  userData.value = {
    ...userData.value,
    name: '李四',
    role: userData.value.role === 'admin' ? 'user' : 'admin'
  }
}

const updateTypedObject = () => {
  typedObject.name = 'TypeScript 更新后'
  typedObject.age += 1
}

const focusInput = () => {
  inputRef.value?.focus()
}

const getInputValue = () => {
  inputValue.value = inputRef.value?.value || ''
}

// 定义 PropsDemo 组件
const PropsDemo = {
  props: {
    userData: {
      type: Object,
      required: true
    },
    count: {
      type: Number,
      default: 0
    },
    active: {
      type: Boolean,
      default: false
    }
  },
  template: `
    <div class="props-display">
      <h4>Props 数据展示</h4>
      <div>用户: {{ userData.name }} ({{ userData.role }})</div>
      <div>计数: {{ count }}</div>
      <div>状态: {{ active ? '激活' : '非激活' }}</div>
    </div>
  `
}
</script>

<style scoped>
.typescript-demo {
  padding: 2rem;
  max-width: 1200px;
  margin: 0 auto;
}

.typescript-demo h2 {
  color: #3b82f6;
  text-align: center;
  margin-bottom: 2rem;
  font-size: 2rem;
}

.ts-features {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
  gap: 2rem;
}

.feature-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

.feature-card h3 {
  background: linear-gradient(135deg, #8b5cf6, #a78bfa);
  color: white;
  margin: 0;
  padding: 1rem;
  font-size: 1.25rem;
}

.content {
  padding: 1.5rem;
}

.content p {
  color: #64748b;
  line-height: 1.6;
  margin-bottom: 1.5rem;
}

.code-block {
  background: #1e293b;
  border-radius: 8px;
  padding: 1rem;
  overflow-x: auto;
  margin-bottom: 1.5rem;
}

.code-block code {
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 0.875rem;
  color: #e2e8f0;
  line-height: 1.5;
}

.demo {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.btn-primary {
  background: #8b5cf6;
  color: white;
  border: none;
  padding: 0.75rem 1.5rem;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: background-color 0.2s;
}

.btn-primary:hover {
  background: #7c3aed;
}

.btn-secondary {
  background: #64748b;
  color: white;
  border: none;
  padding: 0.75rem 1.5rem;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: background-color 0.2s;
}

.btn-secondary:hover {
  background: #475569;
}

.props-display {
  background: #f8fafc;
  border-radius: 8px;
  padding: 1rem;
  border: 1px solid #e2e8f0;
}

.props-display h4 {
  margin-top: 0;
  color: #334155;
}

.props-display div {
  margin-bottom: 0.5rem;
  color: #64748b;
}

.input-field {
  padding: 0.75rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 1rem;
  width: 100%;
}

.input-field:focus {
  outline: none;
  border-color: #8b5cf6;
  box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.1);
}

.result {
  padding: 1rem;
  background: #f1f5f9;
  border-radius: 6px;
  border-left: 4px solid #8b5cf6;
}

@media (max-width: 768px) {
  .typescript-demo {
    padding: 1rem;
  }
  
  .ts-features {
    grid-template-columns: 1fr;
  }
  
  .typescript-demo h2 {
    font-size: 1.75rem;
  }
}
</style>